#coding=utf-8
'''
-脚本注释
-完善脚本
-利用dict改写shopping.py
'''
#import re

class bcolors:
    HEADER = '\033[95m'
    OK_BLUE = '\033[94m'
    OK_GREEN = '\033[92m'
    WARING_YELLOW = '\033[93m'
    FAIL = '\033[36m'
    FLASHING = '\033[35m'
    CRITICAL_RED = '\033[31m'
    END = '\033[0m'

#商品字典
shopping_dict = {'xiaomi':'1999', 'iphone':'4999', 'iwatch':'3499', 'ipad':'2999'}

#接受用户工资，初始化工资和余额
salary = int(raw_input(bcolors.OK_BLUE + "please input your salary：" + bcolors.END))
remain = salary
print "your current balance is: %s" % remain

#初始化一个空字典作为购物车
boughtlist = []

while True:

	#打印商品和对应价格
	print "==========shopping list=========="
	for item in shopping_dict.keys():
		print "%s --- %s" %(item, shopping_dict[item])
	print "=================================\n"

	item = raw_input('which one do you want to buy? ').strip()

	#如果用户购买产品不在商品列表，给出提示
	if item not in shopping_dict.keys():
		print "item not find %s, please check input" % item
		continue

	#如果用户余额小于商品里价格最低的商品价格，提示余额不足并退出
	elif remain < int(min(shopping_dict.values())):
		print "Sorry, low balance, you can't buy anyone in shopping list. "
		action_low_balance = raw_input("press any to exit. ")
		break

	#进入正常购买常规流程
	else:
		print "your bought: %s, price: %s" % (item, shopping_dict[item])

		#计算余额
		remain = remain - int(shopping_dict[item])
		#购买商品加入到购物车
		boughtlist.append(item)

		print "your current balance is: %s" % remain

		#提示用户退出或者继续在购买一次完成之后
		action_curr_balance = raw_input("press any to continue, or EXIT to quit: ")
		action_str = 'exit'

		#忽略掉用户输入大小写，退出打印购物车，否则继续
		if action_curr_balance.lower() == action_str:
			print "Thank you for bought, bye-bye."
			print "the boughtlist: "
			for i in boughtlist:
				print "-> " + i, shopping_dict[i]
			break
		else:
			continue
	break



